package com.hmkj.admin.controller.operate;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.entity.export.ExportUtils;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.banner.BannerModel;
import com.hmkj.core.po.banner.Banner;
import com.hmkj.core.service.banner.BannerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@RestController
@RequestMapping(BaseController.BASE_URI)
public class BannerJsonController  extends GenericController<Integer, Banner> {

    @Resource
    private BannerService bannerService;

    /**
     * 列表json数据
     *
     * @param dtGridPager
     * @return
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/banner/json/list")
    @RequiresPermissions("sys:banner:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
//        DtGrid dtgrid = dataAuthority(dtGridPager, null);
        return bannerService.getDtGridList(dtGridPager, BannerModel.class);
    }

    /**
     * 新增
     * @param banner
     * @return
     */
    @ResponseBody
    @RequestMapping("/banner/json/add")
    @RequiresPermissions("sys:banner:save")
    public ResultEntity addBanner(Banner banner) {
        checkBanner(banner);
        banner.setCreateTime(new Date());
        banner.setIsPc(Banner.ISPC.T1.code);
        banner.setDelFlag(Banner.DELFLAG.NORMAL.code);
        bannerService.insert(banner);
        return ok();
    }

    /**
     * 新增BannerAPP
     * @param banner
     * @return
     */
    @ResponseBody
    @RequestMapping("/banner/json/add1")
    @RequiresPermissions("sys:bannerApp:save")
    public ResultEntity addBannerApp(Banner banner) {
        checkBanner(banner);
        banner.setCreateTime(new Date());
        banner.setIsPc(Banner.ISPC.T2.code);
        banner.setDelFlag(Banner.DELFLAG.NORMAL.code);
        bannerService.insert(banner);
        return ok();
    }

    /**
     * 编辑
     *
     * @param banner
     * @return
     */
    @RequestMapping("/banner/json/edit")
//    @RequiresPermissions("sys:banner:edit")
    public ResultEntity editBanner(Banner banner, Integer id) {
        banner.setId(id);
        Banner persistBanner = bannerService.selectByPrimaryKey(id);
        if (null == persistBanner || persistBanner.getDelFlag() == Banner.DELFLAG.DELETED.code) {
            throw new BussinessException("banner不存在或者已被删除");
        }
        checkBanner(banner);
        banner.setUpdateTime(new Date());
        banner.setDelFlag(Banner.DELFLAG.NORMAL.code);
        bannerService.updateByPrimaryKeySelective(banner);
        return ok();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/banner/json/del")
//    @RequiresPermissions("sys:banner:edit")
    public ResultEntity deleteBanner(Integer id) {
        Banner persistBanner = bannerService.selectByPrimaryKey(id);
        if (null == persistBanner || persistBanner.getDelFlag() == Banner.DELFLAG.DELETED.code) {
            throw new BussinessException("banner不存在或者已被删除");
        }
        bannerService.removeById(persistBanner);
        return ok();
    }

    /**
     * 检查Banner合法性
     *
     * @param banner
     */
    private void checkBanner(Banner banner) {
         if (StringUtils.isEmpty(banner.getTitle())) {
            throw new BussinessException("请输入标题");
        }
        else if(banner.getTitle().length()>20){
             throw new BussinessException("标题长度不能超过20位");
         }
        else if (StringUtils.isEmpty(banner.getPicUrl())){
            throw new BussinessException("请上传图片");
        }
        else if (null == banner.getSort()) {
            throw new BussinessException("请输入排序值");
        }
        else if (StringUtils.isEmpty(banner.getLinkUrl())){
            throw new BussinessException("请输入跳转链接");
        }
        else if(StringUtils.isEmpty(banner.getIsShow())){
            throw new BussinessException("是否显示不能为空");
         }
        else if(banner.getIsShow() != Banner.ISSHOW.T1.code && banner.getIsShow() != Banner.ISSHOW.T2.code){
            throw new BussinessException("是否显示状态错误");
         }
    }

    /**
     * 列表导出
     *
     * @param dtGridPager
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "banner/json/export", method = RequestMethod.POST)
    public ResultEntity export(String dtGridPager, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 执行导出
        ExportUtils.export(request, response, bannerService.getDtGridListExport(dtGridPager));
        return ok();
    }
}
